package com.example.messagingrabbitmq;

import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;

    }


    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        String msg = "Hello from RabbitMQ!";
        String routingKey = "foo.bar.baz";
        String topicExchangeName = MessagingRabbitmqApplication.topicExchangeName;

        rabbitTemplate.convertAndSend(topicExchangeName, routingKey, msg);

        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);

    }

}
